package com.etonenet.controller.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import com.etonenet.entity.system.SystemNode;
import com.etonenet.repository.system.SystemNodeRepository;
import com.etonenet.service.system.security.CustomUser;
import com.etonenet.util.SpringUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import esms.etonenet.boss1069.util.TilesUtil;
import esms.etonenet.boss1069.web.UrlConstants;
import esms.etonenet.boss1069.web.decorate.Menu;
import esms.etonenet.boss1069.web.decorate.MenuItem;
import esms.etonenet.boss1069.web.decorate.MenuRoot;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@Controller
public class IndexController {

	@Resource
	private SystemNodeRepository nodeEm;

	@Resource
	private RestTemplate restTemplate;

	@RequestMapping("test")
	public void test() {
		System.out.println(restTemplate);
	}

	@RequestMapping(value = "/boss")
	public ModelAndView boss() {

		return new ModelAndView("frame:boss");
	}

	@RequestMapping(value = "/home")
	public ModelAndView home() {

		ModelAndView mv = new ModelAndView(TilesUtil.getPath("/frame"));
		List<SystemNode> roots = nodeEm.listAllRoot();
		mv.addObject("roots", roots);

		List<MenuRoot> mrs = new ArrayList<>();
		// 系统设置
		MenuRoot mr = new MenuRoot();
		List<Menu> menu = new ArrayList<>();
		List<MenuItem> items = new ArrayList<>();

		Menu m = new Menu();
		MenuItem mi = new MenuItem();

		mr.setId("0");
		m.setId("0");
		m.setText("系统管理");

		mi.setId("0");
		mi.setText("菜单管理");
		mi.setHref(UrlConstants.SYSTEM_NODE_INDEX);
		items.add(mi);
		mi = new MenuItem();
		mi.setId("1");
		mi.setText("日志管理");
		mi.setHref(UrlConstants.SYSTEM_LOG_INDEX);
		items.add(mi);
		mi = new MenuItem();
		mi.setId("2");
		mi.setText("权限管理");
		mi.setHref(UrlConstants.SYSTEM_AUTH_INDEX);
		items.add(mi);

		m.setItems(items);
		menu.add(m);
		mr.setMenu(menu);
		mrs.add(mr);

		for (SystemNode n : roots) {
			mr = new MenuRoot();
			mr.setId(n.getSystemNodeId().toString());

			menu = new ArrayList<>();
			for (SystemNode n2 : nodeEm.listChildren(n.getSystemNodeId())) {
				m = new Menu();
				m.setId(n2.getSystemNodeId().toString());
				m.setText(n2.getNodeName());

				items = new ArrayList<>();
				for (SystemNode n3 : nodeEm.listChildren(n2.getSystemNodeId())) {
					mi = new MenuItem();
					mi.setId(n3.getSystemNodeId().toString());
					mi.setText(n3.getNodeName());
					mi.setHref(n3.getUrl());

					items.add(mi);
				}
				m.setItems(items);
				menu.add(m);
			}
			mr.setMenu(menu);
			mrs.add(mr);
		}

		mv.addObject("tiles", "template:home");
		mv.addObject("currentUser", ((CustomUser) SpringUtil.getUser()).getUser());
		try {
			mv.addObject("menu", new ObjectMapper().writeValueAsString(mrs));
		} catch (JsonProcessingException e) {
		}
		return mv;
	}

	@RequestMapping("/login")
	public ModelAndView login() {
		ModelAndView mv = new ModelAndView("frame:/login/login");
		mv.addObject("tiles", "template:login");
		return mv;
	}

	@RequestMapping(value = "/welcome")
	public ModelAndView welcome() {

		return new ModelAndView(TilesUtil.getPath("/welcome"));
	}

	@RequestMapping(value = "/boss/api")
	public ModelAndView api() {

		ModelAndView mv = new ModelAndView(TilesUtil.getPath("/api"));
		mv.addObject("accessToken", ((CustomUser) SpringUtil.getUser()).getOauthToken());
		return mv;
	}

	@RequestMapping(value = "/403")
	public ModelAndView e403() {

		return new ModelAndView("403");
	}

	@RequestMapping(value = "/404")
	public ModelAndView e404() {

		return new ModelAndView("404");
	}

	@RequestMapping(value = "/500")
	public ModelAndView e500() {

		return new ModelAndView("500");
	}
}
